/*
 * FeedDataComparator.java
 *
 * Created on August 24, 2007, 2:54 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.afaker.rss.gui.table;

import java.util.Comparator;
import java.util.Date;
import java.util.Vector;

/**
 *
 * @author bruce
 */
public class FeedDataComparator implements Comparator{
    private int sortIndex;
    private boolean sortAsc;
    /** Creates a new instance of FeedDataComparator */
    public FeedDataComparator(int sortIndex,boolean asc) {
        this.sortIndex = sortIndex;
        sortAsc = asc;
    }
    
    @Override
    public int compare(Object o1, Object o2) {
        if(!(o1 instanceof SortableTableModel.VectorWithRow) ||
                !(o2 instanceof SortableTableModel.VectorWithRow))
            return 0;
        Vector v1 = ((SortableTableModel.VectorWithRow) o1).v;
        Vector v2 = ((SortableTableModel.VectorWithRow) o2).v;
        int result = 0;
        Object obj1 = v1.elementAt(sortIndex);
        Object obj2 = v2.elementAt(sortIndex);
        if(obj1 instanceof Boolean) {
            Boolean b1 = (Boolean) obj1;
            Boolean b2 = (Boolean) obj2;
            if(b1)
                result = 1;
            else
                result = -1;
        } else if(obj1 instanceof String) {
            String s1 = (String) obj1;
            String s2 = (String) obj2;
            result = s1.compareTo(s2);
        } else if(obj1 instanceof Date){
            Date d1 = (Date) obj1;
            Date d2 = (Date) obj2;
            long l1 = d1.getTime();
            long l2 = d2.getTime();
            result = l1 < l2 ? -1 : (l1 > l2 ? 1 : 0);
        }
        if (!sortAsc)
            result = -result;
        return result;
    }
    
}
